package LambdaDemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.HashMap;

/**
 * @Author: Lenovo
 * @Date: 2024/8/8 13:01
 * @description:
 */
@FunctionalInterface
interface IA {
    void test();
    //    void test22();
    default void test2() {

    }
    public static void test3() {

    }
}
//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}
//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}
//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}
public class Test {
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("hello",3);
        map.put("abc",5);
        map.put("def",2);

        /*map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key: "+s +" val: "+integer);
            }
        });*/

        map.forEach((key,val)->System.out.println("key: "+key +" val: "+val));

    }
    public static void main8(String[] args) {
        ArrayList <String> arrayList = new ArrayList<>();
        arrayList.add("abc");
        arrayList.add("xyz");
        arrayList.add("isa");
        arrayList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        arrayList.forEach(s -> System.out.println(s));
        System.out.println("===============");
        arrayList.sort((o1, o2) -> o1.compareTo(o2));
        arrayList.forEach(s->{System.out.println(s);});
    }
    public static void main7(String[] args) {
        ArrayList <String> arrayList = new ArrayList<>();
        arrayList.add("abc");
        arrayList.add("xyz");
        arrayList.add("isa");
        arrayList.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("======");
        arrayList.forEach(s -> System.out.println(s));
    }
    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn = (a,b) -> a*b;
        System.out.println(moreParameterReturn.test(10, 20));
    }

    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn = a -> a+3;
        System.out.println(oneParameterReturn.test(1));
    }
    public static void main4(String[] args) {
        NoParameterReturn noParameterReturn = ()-> {
            return 10;
        };
        System.out.println(noParameterReturn.test());
        System.out.println("==========");
        NoParameterReturn noParameterReturn1 = () -> 10;
        System.out.println(noParameterReturn1.test());
    }
    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn = (int a,int b) -> {
            System.out.println("a+b:"+(a+b));
        };
        moreParameterNoReturn.test(10,20);
        System.out.println("===========");
        MoreParameterNoReturn moreParameterNoReturn1 = (a,b) -> {
            System.out.println("a+b:"+(a+b));
        };
        moreParameterNoReturn1.test(10,20);

    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = new OneParameterNoReturn() {
            @Override
            public void test(int a) {
                System.out.println(a);
            }
        };
        oneParameterNoReturn.test(10);
        System.out.println("=========");
        //OneParameterNoReturn oneParameterNoReturn1 = (int a) -> {System.out.println(a);};
        OneParameterNoReturn oneParameterNoReturn1 = a -> System.out.println(a);
        oneParameterNoReturn1.test(10);
    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("重写test方法");
            }
        };
        noParameterNoReturn.test();
        System.out.println("=============");
        NoParameterNoReturn noParameterNoReturn1 = ()-> System.out.println("使用lambda表达式重写test方法");
        noParameterNoReturn1.test();
    }
}
